package com.jiazhong.cloud.sentinel.consumer.controller;

import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.jiazhong.cloud.sentinel.consumer.exception.BlockExceptionPlanB;
import com.jiazhong.cloud.sentinel.consumer.service.SentinelConsumerService;
import jakarta.annotation.Resource;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RequestMapping("/sentinel_consumer")
@RestController
public class SentinelConsumerController {
    @Resource
    private SentinelConsumerService sentinelConsumerService;

    @GetMapping("/a")
    public String a() {
        return sentinelConsumerService.a();
    }

    @GetMapping("/b")
    public String b() {
        return sentinelConsumerService.b();
    }

    @GetMapping("/c")
    public String c() {
        return "This is SentinelConsumerController`c method!";
    }

    @GetMapping("/d1")
    public String d1() {
        return sentinelConsumerService.d();
    }

    @GetMapping("/d2")
    public String d2() {
        return sentinelConsumerService.d();
    }

    @SneakyThrows
    @GetMapping("/e")
    public String e() {
        Thread.sleep(100);
        return "This is SentinelConsumerController`e";
    }

    @GetMapping("/f")
    public String f() {
        int a = 9 / 0;
        return "This is SentinelConsumerController`f";
    }

    @GetMapping("/g")
    @SentinelResource(value = "messageG")
    public String g(@RequestParam("name") String name, @RequestParam("age") int age) {
        return "This is SentinelConsumerController`g!name:" + name + ",age:" + age;
    }

    @GetMapping("/h")
    public String h(@RequestParam("origin") String origin) {
        return "This is SentinelConsumerController`h";
    }

    @GetMapping("/i")
    //  @SentinelResource(value = "messageI", blockHandler = "planB")
    @SentinelResource(value = "messageI",
            blockHandler = "abc", blockHandlerClass = BlockExceptionPlanB.class,
            fallback = "ccc", fallbackClass = BlockExceptionPlanB.class)
    public String i(@RequestParam(defaultValue = "1", value = "num") int num) {
        log.info("用户执行了i方法");
        int a = 9 / num;
        return "This is SentinelConsumerController`i";
    }

    public String planB(int num, BlockException e) {
        log.info("执行了planB方案");
        return "This is SentinelConsumerController`planB";
    }

    public String planC(int num, Throwable e) {
        return "This is SentinelConsumerController`planC";
    }
}
